<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');

include_once (DS . "backoffice/include/header.php");
include_once( DS . "backoffice/include/toppanel_stage.php");

if (isset($_GET['uid']) && !empty($_GET['uid']))
{
    $stage = new Stage($_GET['uid']);
    $offre = new Offre($stage->getIdOffre());
    $etudiant = new Etudiant($stage->getIdEtudiant());
    $enseignants = Enseignant::getListTuteursAsc();
    $entreprise = new Entreprise($stage->getIdEntreprise());
    $tuteurEntreprise = new ContactEntreprise($stage->getIdTuteurEntreprise());
    
    if (isset($_POST['enseignant']))
    {
        $stage->setIdTuteurEnseignant($_POST['enseignant']);
        $stage->updateStage();
        
        $error = PromoCritere::updateNbEtudiant($stage, $_POST['enseignant']);
        if($error == null)
            $noterror = 1;
    }
?>
    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Attribution d'un tuteur enseignant</h3>
                <?php if ($error): ?>
                    <div class="error"><?php echo $error; ?></div>
                <?php endif; ?>
                <?php if($noterror == 1): ?>
                    <div class="success">Le tuteur enseignant a bien &eacute;t&eacute; attribu&eacute;.</div>
                <?php endif; ?>
                <form id="form" method="post" >
                    <fieldset id="personal" style="min-height:650px;">
                        <legend>Informations</legend>
                        <?php echo $stage->getIdOffre(); ?> lol
                            <?php echo $offre->getIdPromo(); ?>
                        <label for="enseignant">Enseignant : </label>
                        <select id="select-enseignant" name="enseignant">
<?php




    foreach ($enseignants as $id => $enseignant)
    {
        

?>

                            <option <?php if( $stage->getIdTuteurEnseignant() == $enseignant->IdUtilisateur ){echo 'selected';} ?> onmouseover="getInformations(this)" id="option-<?php echo $enseignant->IdUtilisateur; ?>" value="<?php echo $enseignant->IdUtilisateur; ?>"><?php echo stripcslashes($enseignant->PrenomUtilisateur) . " " . stripcslashes($enseignant->NomUtilisateur); ?> </option>

<?php
                    }
                    
                    
                    
                    
?>
                    </select>
                    <fieldset style="position:absolute; float:left; margin-top:5px; width:200px;">
                        <legend>Aide pour le choix du tuteur</legend>
                        <div id="propositions">
                            

                            <legend>Technologie du stage</legend>
                            
                            <div style="margin-left:20px;">
                            <?php 
                            $lstTechnos = $offre->getIdsTechnologie();
                            if(!empty($lstTechnos))
                            {
                                foreach($lstTechnos as $idTechno)
                                {
                                    echo "<li>".Technologie::getNomById((int)$idTechno)."</li>";
                                }
                            }
                            ?>
                            </div>
                            <legend>Lieu du stage</legend>
                            <div style="margin-left:20px;">
                            <li>Ville : <b><?php echo $entreprise->getVilleEntreprise(); ?></b></li>
                            <li>Code Postal : <b><?php echo $entreprise->getCPEntreprise(); ?></b></li>
                            </div>
                            
                            <legend>Categorie du stage</legend>
                            <div style="margin-left:20px;">
                            <?php
                            $lstCategorie = $offre->getIdsCategorie();
                            if(!empty($lstCategorie))
                            {
                                foreach($lstCategorie as $idCat)
                                {
                                    echo "<li>".Categorie::getNomById((int)$idCat)."</li>";
                                }
                            }  
                            ?>
                            </div>
                            <legend>Categorie du stage</legend>
                            <div style="margin-left:20px;">
                                
                                
                            </div>    
                        </div>
                       
                    </fieldset>
                    <div id="informations-enseignant" style=" margin-left: 50%; width:300px;">
<?php

$idT = $stage->getIdTuteurEnseignant();
if( !empty($idT) && $idT > 0 )
{
    
                    $enseignant = new Enseignant($stage->getIdTuteurEnseignant());
}
else
{
    $enseignant = new Enseignant($enseignants[0]->IdUtilisateur);
}
                    $lstCriteres = $enseignant->getListCritere();
?>
                        <b>Nom : </b>
<?php echo $enseignant->getNomUtilisateur(); ?>
                        <br/>
                        <b>Pr&eacute;nom : </b>
<?php echo $enseignant->getPrenomUtilisateur(); ?>
                        <br/>
                        <b>Message personnel : </b>

                        
<?php
                    $message = $enseignant->getMessage();
                    if (!empty($message))
                    {
                        ?><br/><i>
                            &laquo;<?php
                        echo html_entity_decode(stripcslashes($message));
                        ?>&raquo;
                        </i><?php
                    }
                    else
                    {
                        echo "Aucun";
                    }
?>
                            
                        <br/>
                        <b>Crit&egrave;res :</b>
                        <br/>
                        <div style="float:left;">Promo</div>
                        <div style="width:250px; margin-left:90px;">
                            <ul>
<?php
                    foreach ($lstCriteres[1] as $id => $details)
                    {
?>
                                <li><?php
                                $promo = new Promo($id);
                                echo stripcslashes($promo->getNom() . ", " . $details['NbEtudiantMax'] . " &eacute;tudiants max, " . $details['NbEtudiant'] . " &eacute;tudiants actuels.");
?></li>
                                    <?php
                                }
                                    ?>
                            </ul>
                        </div>
                        <br/>
                        <div style="float:left;">Lieux/Zones</div>
                        <div style="width:250px; margin-left:90px;">
                            <ul>
<?php
                                foreach ($lstCriteres[2] as $id => $nom)
                                {
?>
                                    <li>
<?php
                                    global $zones;
                                    echo stripcslashes($zones[$id]['nom']);
?>
                                </li>
<?php
                                }
?>
                            </ul>
                        </div>
                        <br/>
                        <div style="float:left;">Technologie</div>
                        <div style="width:250px; margin-left:90px;">
                            <ul>
<?php
                                
                                    
                                    {
                                        echo $i;
                                    }
                                foreach ($lstCriteres[3] as $id => $nom)
                                {
                                    
?>                                  
                                    
                                    <li><?php $techno = new Technologie($id);
                                    echo stripcslashes($techno->getNomTechnologie()); ?></li>
                                   
                                    
                                    <?php
                                    
                                    //echo $id;
                                    //$res = $tab[$i];
                                    //$i++;
                                    //echo $res;
                                    
                                    //echo $tab[1];
                                    //echo $tab[2];
                                }

                                    ?>
                            </ul>
                        </div>
                        <br/>
                        <div style="float:left;">Cat&eacute;gorie</div>
                        <div style="width:250px; margin-left:90px;">
                            <ul>
<?php
                                foreach ($lstCriteres[4] as $id => $nom)
                                {
?>
                                    <li><?php $cat = new Categorie($id);
                                    echo stripcslashes($cat->getNomCategorie()); ?></li>
                                    <?php
                                }
                                    ?>
                            </ul>
                        </div>
                        <br/>
                        <div style="float:left;">Domaine</div>
                        <div style="width:250px; margin-left:90px;">
                            <ul>
<?php
                                foreach ($lstCriteres[5] as $id => $nom)
                                {
?>
                                    <li><?php $dom = new DomaineApplication($id);
                                    echo stripcslashes($dom->getNomDomaineApplication()); ?></li>
                                    <?php
                                }
                                    ?>
                            </ul>
                        </div>
                    </div>
                </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Attribuer" />
                </div>
            </form>
            <script type="text/javascript">
                var divAttribution = document.getElementById("propositions");
                $.getJSON("./",
                {m:"ajaxAttribution", oid:<?php echo $stage->getIdOffre(); ?>, pid:<?php echo $offre->getIdPromo(); ?>}, 
                function(data)
                {
                    divAttribution.innerHTML = "";
                    var contenu = "";
                    for(var i in data.enseignants)
                    {
                        contenu += "<div id=\"enseignant-"+data.enseignants[i].id+"\">";
                        contenu += "<div>";
                        contenu += "<b>Nom : </b> "+data.enseignants[i].nom+"<br/>";
                        contenu += "<b>Pr&eacute;nom : </b> "+data.enseignants[i].prenom+"<br/>";
                        contenu += "<b>Nombre de crit&egrave;res communs : </b> "+data.enseignants[i].nbCriteresCommuns+"<br/>";
                        contenu += "<b>Message : </b> "+data.enseignants[i].message+"<br/>";
                        contenu += "</div>";
                        contenu += "<div style=\"text-align: right;\"><a href=\"javascript: getInformationsById('"+data.enseignants[i].id+"');\">Infos</a> | <a href=\"javascript: select('"+data.enseignants[i].id+"'); getInformationsById('"+data.enseignants[i].id+"');\">S&eacute;lectionner</a></div>";
                        contenu += "</div>";
                        contenu += "<hr/>";
                    }
                    divAttribution.innerHTML = contenu;
                }
            )
            </script>
            <script type="text/javascript">
                function select(id)
                {
                    var select = document.getElementById("select-enseignant");
                    var option = document.getElementById("option-" + id );
                    select.selectedIndex = option.index;
                }

                function getInformationsById(id)
                {
                    $.getJSON(
                    './',
                    {m:"ajaxGetEnseignantById", id:id},
                    function(data){
                        if (!data.error) {
                        }
                        else {
                            var div = document.getElementById("informations-enseignant");
                            div.innerHTML = "";
                            div.style = "margin-left: 50%; width:350px;";
                            var element = document.createElement("b");
                            element.appendChild(document.createTextNode("Nom : "));
                            div.appendChild(element);
                            var valeur = $("<div>").html(data.nom).text();
                            div.appendChild(document.createTextNode(valeur));
                            div.appendChild(document.createElement("br"));

                            element = document.createElement("b");
                            element.innerHTML = "Pr&eacute;nom : ";
                            div.appendChild(element);
                            var valeur = $("<div>").html(data.prenom).text();
                            div.appendChild(document.createTextNode(valeur));

                            div.appendChild(document.createElement("br"));

                            element = document.createElement("b");
                            element.appendChild(document.createTextNode("Message personnel : "));
                            div.appendChild(element);
                            var valeur = $("<div>").html(data.message).text();
                            if( valeur == "" )
                            {
                                div.appendChild(document.createTextNode("Aucun"));
                            }
                            else
                            {
                                div.appendChild(document.createElement("br"));
                                var bI = document.createElement("i");
                                bI.innerHTML = "<i>&laquo; " + valeur + " &raquo;</i>";
                                div.appendChild(bI);
                            }

                            div.appendChild(document.createElement("br"));


                            // ----------------------------------- Criteres -----------------------------------
                            element = document.createElement("b");
                            element.innerHTML = "Crit&egrave;res : ";
                            div.appendChild(element);

                            if( data.criteres.length == 0 )
                            {
                                div.appendChild(document.createTextNode("Aucun"));
                            }
                            else
                            {
                                div.appendChild(document.createElement("br"));
                                for(var typeCritere in data.criteres)
                                {
                                    for(var critere in data.criteres[typeCritere])
                                    {
                                        switch( critere )
                                        {
                                            case "1":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Promo"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( promo in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][promo].nom + ", " + data.criteres[typeCritere][critere][promo].nbEtudiantMax + " &eacute;tudiants max, " + data.criteres[typeCritere][critere][promo].nbEtudiant + " &eacute;tudiants actuels.";
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "2":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Lieux/Zones"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");

                                                for( zone in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][zone].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "3":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Technologie"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( techno in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][techno].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "4":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.innerHTML = "Cat&eacute;gorie";

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( cat in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][cat].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "5":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Domaine"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( dom in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][dom].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    });
                }

                function getInformations(option)
                {
                    $.getJSON(
                    './',
                    {m:"ajaxGetEnseignantById", id:option.value},
                    function(data){
                        if (!data.error) {
                        }
                        else {
                            var div = document.getElementById("informations-enseignant");
                            div.innerHTML = "";
                            div.style = "margin-left: 50%; width:350px;";
                            var element = document.createElement("b");
                            element.appendChild(document.createTextNode("Nom : "));
                            div.appendChild(element);
                            var valeur = $("<div>").html(data.nom).text();
                            div.appendChild(document.createTextNode(valeur));
                            div.appendChild(document.createElement("br"));

                            element = document.createElement("b");
                            element.innerHTML = "Pr&eacute;nom : ";
                            div.appendChild(element);
                            var valeur = $("<div>").html(data.prenom).text();
                            div.appendChild(document.createTextNode(valeur));

                            div.appendChild(document.createElement("br"));

                            element = document.createElement("b");
                            element.appendChild(document.createTextNode("Message personnel : "));
                            div.appendChild(element);
                            var valeur = $("<div>").html(data.message).text();
                            if( valeur == "" )
                            {
                                div.appendChild(document.createTextNode("Aucun"));
                            }
                            else
                            {
                                div.appendChild(document.createElement("br"));
                                var bI = document.createElement("i");
                                bI.innerHTML = "<i>&laquo; " + valeur + " &raquo;</i>";
                                div.appendChild(bI);
                            }

                            div.appendChild(document.createElement("br"));


                            // ----------------------------------- Criteres -----------------------------------
                            element = document.createElement("b");
                            element.innerHTML = "Crit&egrave;res : ";
                            div.appendChild(element);

                            if( data.criteres.length == 0 )
                            {
                                div.appendChild(document.createTextNode("Aucun"));
                            }
                            else
                            {
                                div.appendChild(document.createElement("br"));
                                for(var typeCritere in data.criteres)
                                {
                                    for(var critere in data.criteres[typeCritere])
                                    {
                                        switch( critere )
                                        {
                                            case "1":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Promo"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( promo in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][promo].nom + ", " + data.criteres[typeCritere][critere][promo].nbEtudiantMax + " &eacute;tudiants max, " + data.criteres[typeCritere][critere][promo].nbEtudiant + " &eacute;tudiants actuels.";
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "2":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Lieux/Zones"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");

                                                for( zone in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][zone].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "3":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Technologie"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( techno in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][techno].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "4":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.innerHTML = "Cat&eacute;gorie";

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( cat in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][cat].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                            case "5":
                                                element = document.createElement("div");
                                                element.setAttribute("style", "float:left;");
                                                element.appendChild(document.createTextNode("Domaine"));

                                                div.appendChild(element);

                                                element = document.createElement("div");
                                                element.setAttribute("style", "width:250px; margin-left:90px;");

                                                var ul = document.createElement("ul");
                                                for( dom in data.criteres[typeCritere][critere])
                                                {
                                                    var li = document.createElement("li");
                                                    var text = data.criteres[typeCritere][critere][dom].nom;
                                                    li.innerHTML = text;

                                                    ul.appendChild(li);
                                                }
                                                element.appendChild(ul);
                                                div.appendChild(element);

                                                div.appendChild(document.createElement("br"));
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            </script>
        </div>
    </div>
<?php
                            }
                            else
                            {
?>
                                <div id="wrapper">
                                    <div id="content">
                                        Une erreur est survenue, veuillez r&eacute;essayer.
                                    </div>
<?php
                            }
?>
        <?php
                            include_once (DS . "backoffice/include/sidebar.php");
                            include_once (DS . "backoffice/include/footer.php");
        ?>

